// 
// Decompiled by Procyon v0.5.36
// 

package com.akamai.botman;

import java.util.*;
import android.app.*;
import android.os.*;
import android.hardware.*;

final class o extends java.util.Observable implements android.hardware.SensorEventListener
{
    private android.hardware.SensorManager b;
    private android.hardware.Sensor c;
    private android.hardware.Sensor d;
    private android.hardware.Sensor e;
    private boolean f;
    private boolean g;
    private boolean h;
    private boolean i;
    private boolean j;
    boolean a;
    private long k;
    private float[] l;
    private float[] m;
    
    o(final android.app.Application application) {
        this.f = false;
        this.g = false;
        this.h = false;
        this.i = false;
        this.j = false;
        this.a = false;
        this.k = 0L;
        this.l = new float[3];
        this.m = new float[3];
        try {
            this.b = (android.hardware.SensorManager)application.getSystemService("sensor");
        }
        catch (java.lang.NullPointerException ex) {
            com.akamai.botman.al.c("OrientationListener", "Exception on getting sensor service", ex);
            com.akamai.botman.f.a(ex);
        }
    }
    
    public final boolean a() throws java.lang.Exception {
        final java.lang.Throwable[] array = new java.lang.Throwable[0];
        this.c = this.b.getDefaultSensor(1);
        this.f = this.b.registerListener((android.hardware.SensorEventListener)this, this.c, 3);
        this.d = this.b.getDefaultSensor(2);
        this.g = this.b.registerListener((android.hardware.SensorEventListener)this, this.d, 3);
        if (android.os.Build.VERSION.SDK_INT >= 9) {
            this.e = this.b.getDefaultSensor(9);
            this.h = this.b.registerListener((android.hardware.SensorEventListener)this, this.e, 3);
        }
        if (this.h) {
            this.b.unregisterListener((android.hardware.SensorEventListener)this, this.c);
            this.f = false;
            com.akamai.botman.d.q = 1;
        }
        else {
            com.akamai.botman.d.q = 0;
        }
        this.k = android.os.SystemClock.uptimeMillis();
        if ((this.f || this.h) && this.g) {
            new java.lang.StringBuilder("orientation listener started with accelerometer ").append(this.f).append(" Gravity sensor ").append(this.h).append(" Magnetometer ").append(this.g);
            final java.lang.Throwable[] array2 = new java.lang.Throwable[0];
            return true;
        }
        com.akamai.botman.al.c("OrientationListener", "Failed to register orientation listener", new java.lang.Throwable[0]);
        this.b();
        return false;
    }
    
    public final void b() throws java.lang.Exception {
        final java.lang.Throwable[] array = new java.lang.Throwable[0];
        if (this.g) {
            this.b.unregisterListener((android.hardware.SensorEventListener)this, this.d);
            this.g = false;
        }
        if (this.h) {
            this.b.unregisterListener((android.hardware.SensorEventListener)this, this.e);
            this.h = false;
        }
        if (this.f) {
            this.b.unregisterListener((android.hardware.SensorEventListener)this, this.c);
            this.f = false;
        }
        this.a = false;
    }
    
    public final void onAccuracyChanged(final android.hardware.Sensor sensor, final int accuracy) {
    }
    
    public final void onSensorChanged(final android.hardware.SensorEvent event) {
        try {
            final long uptimeMillis = android.os.SystemClock.uptimeMillis();
            if (!this.a && event.accuracy == 0) {
                com.akamai.botman.al.b("OrientationListener", "Unreliable orientation sensor data...", new java.lang.Throwable[0]);
                this.a = true;
            }
            final int type = event.sensor.getType();
            new java.lang.StringBuilder("Orientation sensor changed elapsed time: ").append(uptimeMillis - this.k).append(", Sensor Type: ").append(type);
            final java.lang.Throwable[] array = new java.lang.Throwable[0];
            if (type == 9) {
                this.l = event.values.clone();
                this.i = true;
            }
            else if (type == 1) {
                this.l = event.values.clone();
                this.i = true;
            }
            else if (type == 2) {
                this.m = event.values.clone();
                this.j = true;
            }
            if (this.i && this.j) {
                new java.lang.StringBuilder("OrientationListener.onSensorChanged ").append(event.timestamp);
                final java.lang.Throwable[] array2 = new java.lang.Throwable[0];
                if (uptimeMillis - this.k >= 100L || com.akamai.botman.d.m == 1) {
                    new java.lang.StringBuilder("Orientation event elapsed time: ").append(uptimeMillis - this.k);
                    final java.lang.Throwable[] array3 = new java.lang.Throwable[0];
                    final boolean b = com.akamai.botman.d.m != 0;
                    com.akamai.botman.d.m = 0;
                    this.k = uptimeMillis;
                    this.setChanged();
                    this.notifyObservers(new com.akamai.botman.n(this.l, this.m, this.k, b ? 2 : 1));
                    this.i = false;
                    this.j = false;
                }
            }
        }
        catch (java.lang.Exception ex) {
            com.akamai.botman.al.b("OrientationListener", "Exception in processing orientation event", ex);
            com.akamai.botman.f.a(ex);
        }
    }
}
